import requests
import os
import json
from bs4 import BeautifulSoup
import re

img_url = 'https://d1.hucdn.com/upload/item/1810/06/09573836565596_800x800.jpg'
base_url = 'https://m.beidian.com/detail/detail.html'
params = {
    "iid": 30765335
}

# https://api.beidian.com/mroute.html
# p={
#     method: beidian.search.item.list
# _abr_: 01143e93712d9169bc05a14e967a4c0eb7f2f362e05bf0cc86
# keyword: 大家电
# sort: hot
# page_size: 20
# page: 1
# }

def img_download(imgUrl, idx, category, filepath):
    try:
        res = requests.get(imgUrl, stream = True)
        with open('F:/taobao/'+ filepath +'/'+ category + str(idx)+'.jpg', 'wb') as outfile:
            for chunk in res.iter_content(chunk_size = 32):
                outfile.write(chunk)
        print('download ' + imgUrl)
    except Exception as e:
        print('error')
        print(e)



def goodsDetail(param):
    try:
        res = requests.get(base_url, params=param)
        html = BeautifulSoup(res.text, "html.parser")
        title = str(html.select('.product .title p')[0].string)
        titleSplited = re.compile(r"[\u4E00-\u9FA5| a-z]+").findall(title)[0]
        print(titleSplited)
        os.makedirs('F:/taobao/'+ titleSplited, exist_ok= True)
        heads = html.select(' #J_banner .slider-wrap img')
        details = str(html.find_all('script')[0])
        
        detail_pattern = re.compile(r"<section.+section>") #匹配js中商品详情 的正则
        detailContent = detail_pattern.findall(details)[0]
        
        imgUrl = re.compile(r'https://[a-z\d_\.\/]+') # 匹配商品详情中https开头的路径 即img地址
        detail_imgs = re.findall(imgUrl, detailContent)

        specPattern = re.compile(r"\"*'*imgs\"*\'*:\s*\{\s*[\"a-z\d:_\s\/\.,]*}") # 匹配规格图img地址
        specResult = re.findall(specPattern, details)[0]
        specImgPattern = re.compile(r"{\s*[\"a-z\d:_\s\/\.,]*}")
        specImgList = list(json.loads(re.findall(specImgPattern, specResult)[0]).values())

        head_lenth = len(heads)
        detail_lenth = len(detail_imgs)
        spec_length = len(specImgList)

        for i in range(head_lenth):
            if i == 0:
                img_download(heads[i].attrs['src'],i,'head', titleSplited)
            else:
                img_download(heads[i].attrs['dataimg'], i, 'head', titleSplited)

        for j in range(1, detail_lenth):
            img_download(detail_imgs[j], j, 'detail', titleSplited)
        for k in range(spec_length):
            img_download(specImgList[k], k, 'spec', titleSplited)
        print('download finish')
    except Exception as e:
        print('error')
        print(e)

goodsDetail(params)
# chunk_download(img_url)
